import { Injectable } from "@angular/core";
import { Platform } from "@ionic/angular";
import { I18N } from "./i18n.config";
import { LocalStorage } from "./local-storage.service";
import { TemplateConfig } from "./print.const";

@Injectable()
export class Global {

	static FIRST_LAUNCH = "FIRST_LAUNCH";
	static TEMPLATES = "TEMPLATES";
	static APP_ID = "id1554477295";
	static ADMOB_BANNER = "ca-app-pub-3289899656669935/3273389192";
	static ADMOB_INSERT = "ca-app-pub-3289899656669935/5516409155";
	static ADMOB_VIDEO = "ca-app-pub-3289899656669935/6637919135";

	static BD_VIDEO = "945874845";
	static BD_SPLASH = "887443108";

	static IAP = "printer_admob";

	static LANGUAGE = "LANGUAGE";

	constructor(
		private platform: Platform,
		private localStorage: LocalStorage,
	) {
	}

	firstLaunch() {
		if (!this.localStorage.getData(Global.FIRST_LAUNCH)) {
			const date = new Date();
			this.localStorage.setData(Global.FIRST_LAUNCH, {
				date: date.getTime() + 60 * 3600 * 1000
			});
			this.localStorage.setData(Global.TEMPLATES, []);
		}
	}

	get needShowAd(): boolean {
		return !this.localStorage.getData(Global.IAP);
	}

	get isFree(): boolean {
		const date = this.localStorage.getData(Global.FIRST_LAUNCH).date;
		const now = new Date().getTime();
		return now < date;
	}

	get isDevice(): boolean {
		return this.platform.is("cordova");
	}

	chooseLanguage(language: string) {
		this.localStorage.setData(Global.LANGUAGE, language);
	}

	get language() {
		return this.localStorage.getData(Global.LANGUAGE);
	}

	translate(key: string) {
		const language = this.localStorage.getData(Global.LANGUAGE);
		switch (language) {
			case I18N.LANGUAGE_EN_US:
				return I18N.STR_EN_US[key] ? I18N.STR_EN_US[key] : key;
			case I18N.LANGUAGE_CHN_S:
				return I18N.STR_CHN_S[key] ? I18N.STR_CHN_S[key] : key;
			default:
				return I18N.STR_EN_US[key] ? I18N.STR_EN_US[key] : key;
		}
	}

	get templates(): TemplateConfig[] {
		return this.localStorage.getData(Global.TEMPLATES)
	}

	saveTemplate(template: TemplateConfig) {
		const templates = this.templates;
		templates.unshift(template);
		this.localStorage.setData(Global.TEMPLATES, templates);
	}

	removeTemplate(index: number) {
		const templates = this.templates;
		templates.splice(index, 1);
		this.localStorage.setData(Global.TEMPLATES, templates);
	}
}
